#!/usr/bin/env bash

include "libs/ohos.sh"
include "libs/output.sh"

hdc:check_hdc()
{
    if [ "$OHOS_HDC" == "" ]; then
        output:loge "找不到hdc，请执行ohos-tool cc@sdk编译出hdc_std"
        return 1
    fi
}

hdc:root()
{
    hdc:check_hdc || return
    echo "$(cctl y)正在获取root权限$(cctl 0)"
    local result=$($OHOS_HDC smode)
    if [ "$?" = "0" ]; then
        echo "$(cctl g)获取root权限成功: $(cctl c)$result$(cctl 0)"
    fi
}

hdc:shell()
{
    hdc:check_hdc || return
    if [ "$#" != "1" ]; then
        echo "$(cctl y)正在执行命令: $1$(cctl 0)"
    fi
    $OHOS_HDC shell $*
}

hdc:remount()
{
    hdc:check_hdc || return
    echo "$(cctl y)正在重新加载根目录$(cctl 0)"
    hdc:shell mount -o remount,rw / || return
    # local result=$($OHOS_HDC target mount)
    if [ "$?" = "0" ]; then
        echo "$(cctl g)重新加载根目录成功: $(cctl c)$result$(cctl 0)"
    fi
}

hdc:kill()
{
    hdc:check_hdc || return
    killall hdc 1>/dev/null 2>&1
    killall hdc_std 1>/dev/null 2>&1
    $OHOS_HDC kill -r
}

hdc:wait_for_device()
{
    ohos:cd_repo || return
    hdc:check_hdc || return

    local result=""
    while [ "$result" == "" ]; do
        print:circle_slider "waiting for device"
        sleep 0.1
        local start_time=$(date +%s%N)
        result=$($OHOS_HDC list targets)
        [ "${result:0:7}" = "[Empty]" ] && result="" && continue
        [ "${result:0:6}" = "[Fail]" ] && result="" && continue

        end_time=$(date +%s%N)
        time=$(( $end_time - $start_time ))
        if [ $time -gt 1000000000 ]; then
            hdc:kill || return
            sleep 1
        fi
    done
    echo -en "\033[2K" 1>&2
}

hdc:rr()
{
    hdc:wait_for_device || return
    hdc:remount || return
}

hdc:reset()
{
    hdc:shell "stop_service hdcd; killall hdcd; start_service hdcd" || return
}

hdc:version()
{
    hdc:check_hdc || return
    $OHOS_HDC -v
}

function kill_hdc_push()
{
    local filename=$1
    sleep 7
    if [ -e $filename ]; then
        hdc:rr || return
        hdc:reset || return
    fi
}

hdc:push()
{
    hdc:check_hdc || return
    if [ "$OHOS_NORR" != "1" ]; then
        hdc:rr || return
    fi

    hdc:wait_for_device || return
    local result=""
    while [ "$result" = "" ]; do
        print:circle_slider "pushing"

        local tmpfile="/tmp/push-$(cat /proc/sys/kernel/random/uuid)"
        touch "$tmpfile"

        kill_hdc_push $tmpfile &
        result=$($OHOS_HDC file send "$SOURCE_FILE" /data/$PERMISSION)
        [ "${result:0:6}" = "[Fail]" ] && result=""

        rm "$tmpfile"
    done
    echo -e "\033[2K$(cctl g)推送结果: $(cctl c)$result$(cctl 0)"

    if [ "$PERMISSION" != "" ]; then
        SHELL_COMMAND="chmod $PERMISSION /data/$PERMISSION"
        SHELL_COMMAND+="; rm -rf $DIR/$(basename $SOURCE_FILE)"
        SHELL_COMMAND+="; mv /data/$PERMISSION $DIR/$(basename $SOURCE_FILE)"
        SHELL_COMMAND+="; sync"
        hdc:shell "$SHELL_COMMAND" || return
    fi
}

find_push_file()
{
    ohos:check_param FILE || return

    if [ -e "$FILE" ]; then
        SOURCE_FILE="$FILE"
    elif [ -e "$COMMAND_CURDIR/$FILE" ]; then
        SOURCE_FILE="$COMMAND_CURDIR/$FILE"
    else
        ohos:cd_repo || return
        SOURCE_FILE="$(find $OHOS_OUT_DIR/ -type f -name $FILE)"
        [ "$SOURCE_FILE" != "" ] || report "在 $OHOS_OUT_DIR 里找不到$FILE" || return
        SOURCE_FILE="$(echo "$SOURCE_FILE" | xargs ls -lrt | tail -1 | awk '{print $NF}')"
        [ "$SOURCE_FILE" != "" ] || report "在 $OHOS_OUT_DIR 里找不到$FILE" || return
        SOURCE_FILE="$(pwd)/$SOURCE_FILE"
    fi

    if [ "$DIR" == "" ]; then
        PERMISSION=755
        DIR=/system/bin
        if [ "$(echo $FILE | awk '{print match($0, "\\.so$")}')" != "0" ]; then
            DIR=/system/lib
            PERMISSION=644
        fi
        if [ "$(echo $FILE | awk '{print match($0, "\\.rc$")}')" != "0" ]; then
            DIR=/system/etc/init
            PERMISSION=644
        fi
    fi
    PERMISSION=${PERMISSION:-644}

    echo -en "$(cctl y)正在将本地文件推到设备的 $(cctl p)$DIR$(cctl y) 上: "
    echo -e "$(print:pathfile $SOURCE_FILE "\$$CONTAINER_NAME=$REPO_PATH")"
}
